//
//  ViewController.swift
//  work4
//
//  Created by Qingke on 16/3/26.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    
    
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var random: UILabel!
    @IBOutlet weak var cycleLable: UILabel!
    @IBOutlet weak var totalLabel: UILabel!
    @IBOutlet weak var button: UIButton!
    
    var targetN = 0 {
        didSet {
            random.text = String(targetN)
        }
    }
   
    var mag = ""
    
    var cycleNumber = 1 {
        didSet {
            cycleLable.text = String( cycleNumber)
        }
    }
    var totalNumber = 0 {
        didSet {
            totalLabel.text = String( totalNumber)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let button_highlight = UIImage ( named: "Button-Highlighted" )
    
        button.setBackgroundImage(button_highlight, forState: .Highlighted )
        
        
        targetN = randNum()
        random.text = String ( targetN )
        cycleLable.text = String ( cycleNumber )
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func IAbutton(sender: UIButton) {
    
        let (scores, msg) = calcScore()
        let tip = personality(scores)
        mag = msg + tip
        totalNumber += scores
        
       
        let alert = UIAlertController(title:" 测试", message: mag, preferredStyle: .Alert)
        let action = UIAlertAction(title: "我是Action", style:.Cancel , handler: {
            action in
                self.next()
                }
            )
        alert.addAction(action)
        presentViewController ( alert, animated: true, completion: nil )
        
        
    }
    
    @IBAction func newGame(sender: UIButton) {
        targetN = randNum()
        cycleNumber = 1
        totalNumber = 0
    }
    
    
    func next() {
        targetN = randNum()
        cycleUp()
    }
    
    
    func calcScore() -> (Int,String){
        let reN = Int(slider.value)
        let dif = abs(targetN - reN)
        let scores = 100 - dif
        let msg = "你好啊数值是\(reN) \n 需要的值是：\(targetN) \n 差距是：\(dif) \n 成绩是：\(scores)"
        return (scores, msg)
        
    }
    
    
    func personality(score: Int) ->String{
        switch score {
        case 90...100: return "\n good "
        case 70...100: return "\n 干的好well"
        case 50...70: return "\n just try"
        default:return "\n 都说别喝酒了"
        }
    }
    
    
    func randNum() -> Int {
        return Int ( arc4random_uniform(100)+1 )
    }
    
    
    func cycleUp() ->Int {
        cycleNumber +=  1
        print("加1")
        return cycleNumber
    }
    
    @IBAction func goback(segue: UIStoryboardSegue){
        
    }

}

